package com.yang.service;

import com.yang.entity.User;
import com.yang.dao.UserDao;
import com.yang.util.RespEntity;
import com.yang.util.RespUtil;

import java.util.*;

public class UserService {
    private final UserDao userDao = new UserDao();

    //查询全部
    public RespEntity queryByUser() {
        List<User> users = userDao.queryUser();
        System.out.println(users.toString());
       return new RespEntity(200,"查询",users);
    }
    //登录判断账号是否存在，密码是否正确
    public RespEntity queryUserBylogin(String login,String pwd) {
        User loginuser = userDao.queryUserBylogin(login);
        RespEntity respEntity=new RespEntity(200,"验证账号密码",null);;
        if (loginuser!=null) {
            System.out.println(loginuser.getPwd());
            if (pwd.equals(loginuser.getPwd())){
                respEntity.setData(loginuser);
            }
        }
        return respEntity;
    }
    //验证账号是否可用
    public RespEntity yanzhengUser(String name) {
        User login = userDao.queryUserBylogin(name);
        return new RespEntity(200,"验证账号是否可用",login==null?0:1);
    }
    //添加用户
    public RespEntity saveUser(User obj) {
        int i = userDao.saveUser(obj);
        return new RespEntity(200,"添加用户",i);
    }
    //删除
    public int deleteUserById(Integer id) {
        return userDao.deleteUserById(id);
    }

    //修改个人资料
    public RespEntity updateUserById(User user) {
        StringBuffer sb=new StringBuffer("update user set");
        //判断不为空的拼接sql条件
        if (user.getName()!=null&&!"".equals(user.getName())) {
            sb.append(" name='"+user.getName()+"',");
        }
        if (user.getPwd()!=null&&!"".equals(user.getPwd())) {
            sb.append(" pwd='"+user.getPwd()+"',");
        }
        if (user.getSex()!=null){
            sb.append(" sex='"+user.getSex()+"',");
        }
        if (user.getBirthday()!=null){
            sb.append(" birthday='"+user.getBirthday().toString()+"',");
        }
        if (user.getPhone()!=null){
            sb.append(" phone='"+user.getPhone()+"',");
        }
        if (user.getPhoto()!=null){
            sb.append(" photo='"+user.getPhoto()+"',");
        }
        sb.append("master="+user.getMaster());
        sb.append(" where id="+user.getId());
        int num=userDao.updateBySql(sb.toString());

        return new RespEntity(200,"修改",num);
    }

    //html后台添加用户
    public RespEntity HtmlSava(User user) {
        int i = userDao.saveUser(user);
        return new RespEntity(200,"添加用户",i);
    }
    //后台根据id查询
    public RespEntity HtmlQueryById(int id) {
        User user = userDao.queryUserByid(id);
        return new RespEntity(200,"添加用户",user);
    }
    //后台修改
    //修改个人资料
    public RespEntity HtmlUserById(User user) {
        StringBuffer sb=new StringBuffer("update user set");
        //判断不为空的拼接sql条件
        if (user.getLogin()!=null&&!"".equals(user.getLogin())) {
            sb.append(" login='"+user.getLogin()+"',");
        }
        if (user.getName()!=null&&!"".equals(user.getName())) {
            sb.append(" name='"+user.getName()+"',");
        }
        if (user.getPwd()!=null&&!"".equals(user.getPwd())) {
            sb.append(" pwd='"+user.getPwd()+"',");
        }
        if (user.getSex()!=null&&!"".equals(user.getSex())){
            sb.append(" sex='"+user.getSex()+"',");
        }
        if (user.getBirthday()!=null){
            sb.append(" birthday='"+user.getBirthday().toString()+"',");
        }
        if (user.getPhoto()!=null){
            sb.append(" photo='"+user.getPhoto()+"',");
        }

        sb.append("master="+user.getMaster());
        sb.append(" where id="+user.getId());
        System.out.println(sb.toString());
        int num=userDao.updateBySql(sb.toString());

        return new RespEntity(200,"修改",num);
    }
    //后台删除用户
    public RespEntity DeleteUserByid(int id) {
        int sum = userDao.deleteUserById(id);
        System.out.println("dshfkjhk   "+sum);
        return new RespEntity(200,"删除成功",sum);
    }
}
